﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BALayer;
using System.Data.SqlClient;

namespace QuanLyDoan
{
    public partial class FrmTraCuuHoatDong : Form
    {
        DBTTHoatDong db = null;
        DBDoanVien dbDoanVien;
        DBDonViToChuc dbDonViToChuc;

        DataTable dtThongTinHD;
        DataTable dtCanBo;
        DataTable dtDonViToChuc;
        DataTable dtLop;

        public FrmTraCuuHoatDong()
        {
            InitializeComponent();
            db = new DBTTHoatDong();
            dbDoanVien = new DBDoanVien();
            dbDonViToChuc = new DBDonViToChuc();
        }


        void LoadData()
        {
            dtCanBo = new DataTable();
            dtCanBo.Clear();
            dtCanBo = dbDoanVien.LayCanBo().Tables[0];

            dtDonViToChuc = new DataTable();
            dtDonViToChuc.Clear();
            dtDonViToChuc = dbDonViToChuc.LayDonViToChuc().Tables[0];

            dtLop = new DataTable();
            dtLop.Clear();
            dtLop = db.LayLop().Tables[0];

            MaQL.DataSource = dtCanBo;
            MaQL.ValueMember = "MaDV";
            MaQL.DisplayMember = "Ten";

            MaDVTC.DataSource = dtDonViToChuc;
            MaDVTC.DisplayMember = "TenDonVi";
            MaDVTC.ValueMember = "MaDVTC";

            cbChiDoan.DataSource = dtLop;
            cbChiDoan.DisplayMember = "MaLop";
            cbChiDoan.ValueMember = "MaLop";


            //Vận chuyển dữ liệu lên datatable
            dtThongTinHD = new DataTable();
            dtThongTinHD.Clear();
            dtThongTinHD = db.LayTraCuuHD().Tables[0];

            //Đưa dữ liệu lên DataGridView
            dgvTraCuuThongTinHD.DataSource = dtThongTinHD;

            txtTenHD.ResetText();
            txtThang.ResetText();
            txtNam.ResetText();
            cbChiDoan.SelectedIndex = 0;
            
        }
        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnTroVe_Click(object sender, EventArgs e)
        {
                this.Close();
        }

        private void txtTenHD_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string chidoan = cbChiDoan.SelectedValue.ToString();
                string thang = txtThang.Text.ToString();
                string nam = txtNam.Text.ToString();
                string tenhd = txtTenHD.Text.ToString();

                DataTable dt = new DataTable();
                dt.Clear();
                dt = db.TraCuuTTHD(chidoan, thang, nam, tenhd).Tables[0];

                dgvTraCuuThongTinHD.DataSource = dt;

                if (dgvTraCuuThongTinHD.RowCount == 0)
                {
                    MessageBox.Show("Không tồn tại dữ liệu trong CSDL!!!");
                }
            }
        }

        private void FrmTraCuuHoatDong_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnReLoad_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void txtThang_TextChanged(object sender, EventArgs e)
        {
            string chidoan = cbChiDoan.SelectedValue.ToString();
            string thang = txtThang.Text.ToString();
            string nam = txtNam.Text.ToString();
            string tenhd = txtTenHD.Text.ToString();

            DataTable dt = new DataTable();
            dt.Clear();
            dt = db.TraCuuTTHD(chidoan, thang, nam, tenhd).Tables[0];

            dgvTraCuuThongTinHD.DataSource = dt;

            if (dgvTraCuuThongTinHD.RowCount == 0)
            {
                //MessageBox.Show("Không tồn tại dữ liệu trong CSDL!!!");
            }
        }

        private void txtNam_TextChanged(object sender, EventArgs e)
        {
            string chidoan = cbChiDoan.SelectedValue.ToString();
            string thang = txtThang.Text.ToString();
            string nam = txtNam.Text.ToString();
            string tenhd = txtTenHD.Text.ToString();

            DataTable dt = new DataTable();
            dt.Clear();
            dt = db.TraCuuTTHD(chidoan, thang, nam, tenhd).Tables[0];

            dgvTraCuuThongTinHD.DataSource = dt;

            if (dgvTraCuuThongTinHD.RowCount == 0)
            {
                //MessageBox.Show("Không tồn tại dữ liệu trong CSDL!!!");
            }
        }

        private void cbChiDoan_SelectedIndexChanged(object sender, EventArgs e)
        {
            string chidoan = cbChiDoan.SelectedValue.ToString();
            string thang = txtThang.Text.ToString();
            string nam = txtNam.Text.ToString();
            string tenhd = txtTenHD.Text.ToString();

            DataTable dt = new DataTable();
            dt.Clear();
            dt = db.TraCuuTTHD(chidoan, thang, nam, tenhd).Tables[0];

            dgvTraCuuThongTinHD.DataSource = dt;

            if (dgvTraCuuThongTinHD.RowCount == 0)
            {
                //MessageBox.Show("Không tồn tại dữ liệu trong CSDL!!!");
            }
        }

        private void txtTenHD_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string chidoan = cbChiDoan.SelectedValue.ToString();
                string thang = txtThang.Text.ToString();
                string nam = txtNam.Text.ToString();
                string tenhd = txtTenHD.Text.ToString();

                DataTable dt = new DataTable();
                dt.Clear();
                dt = db.TraCuuTTHD(chidoan, thang, nam, tenhd).Tables[0];

                dgvTraCuuThongTinHD.DataSource = dt;

                if (dgvTraCuuThongTinHD.RowCount == 0)
                {
                    MessageBox.Show("Không tồn tại dữ liệu trong CSDL!!!");
                }
            }
        }
    }
}
